﻿// https://leetcode-cn.com/problems/balanced-binary-tree/
// 110. 平衡二叉树

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        return GetDepth(root) == -1 ? false : true;
    }

    int GetDepth(TreeNode *root) {
        if (root == nullptr) return 0;
        int left_depth = GetDepth(root->left);
        if (left_depth == -1) return -1;
        int right_depth = GetDepth(root->right);
        if (right_depth == -1) return -1;
        return abs(left_depth - right_depth) > 1 ? -1 : max(left_depth, right_depth) + 1;
    }
};

int main() {
    Solution obj = Solution();
}